package com.woniu.smart.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.smart.dto.CarpayDTO;
import com.woniu.smart.entity.Carpay;
import com.woniu.smart.entity.Information;
import com.woniu.smart.mapper.*;
import com.woniu.smart.service.CarpayService;
import com.woniu.smart.vo.CarVO;
import com.woniu.smart.vo.CarpayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author ：mmzs
 * @description：TODO
 * @date ：2022/10/21 15:04
 */
@Service
public class CarpayServiceImpl implements CarpayService {
    @Autowired
    private CarpayMapper carpayMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private AccessMapper accessMapper;
    @Autowired
    private StaffsMapper staffsMapper;
    @Autowired
    private InformationMapper informationMapper;

    /**
     * 查询所有汽车缴费信息
     * @return
     */
    @Override
    public List<CarpayVO> selectAll(CarpayDTO carpayDTO) {
        PageHelper.startPage(carpayDTO.getCurrentPage(),carpayDTO.getPageSize());
        return carpayMapper.selectAll(carpayDTO);
    }

    /**
     * 根据车牌号码查找车辆信息和车辆进出信息
     * @param carNum
     * @return
     */
    @Override
    public CarpayVO selectByCarNum(String carNum) {
        List<CarpayVO> list = carpayMapper.selectByCarNum(carNum);
        return list.get(0);
    }

    @Override
    public CarpayVO selectByCarpayId(Long id) {
        return carpayMapper.selectByCarpayId(id);
    }


    /**
     * 删除车辆缴费
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long id) {
        Carpay carpay = carpayMapper.selectByPrimaryKey(id);
        int i = informationMapper.deleteByPrimaryKey((long) carpay.getInformationId());
        int i1 = carpayMapper.deleteByPrimaryKey(id);
        return i+i1;
    }

    @Override
    public int insert(Carpay record) {
        return 0;
    }

    /**
     * 添加车辆缴费信息
     * @param recordVO
     * @return
     */
    @Override
    public int insertCarpaySelective(CarpayVO recordVO) {
        Carpay carpay = new Carpay();
        Information information = new Information();
        List<Integer> list1 = informationMapper.selectPrimaryKey();
        information.setInformationId(list1.get(0)+1);
        List<Integer> list = carpayMapper.selectPrimaryKey();
        carpay.setCarpayId(list.get(0)+1);
        carpay.setCarpayPrice("22");
        carpay.setCarpayIntime(recordVO.getAccessIn());
        carpay.setCarpayOuttime(recordVO.getAccessOut());
        CarVO carVO = carMapper.selectByCarNum(recordVO.getCarNum());
        carpay.setCarId(carVO.getCarId());
        carpay.setInformationId(information.getInformationId());
        int i = carpayMapper.insertSelective(carpay);
        information.setCarpayId(carpay.getCarpayId());
        information.setInformationType(1);
        information.setInformationYn(1);
        information.setInformationNum(UUID.randomUUID().toString());
        information.setInformationMoney(recordVO.getInformationMoney());
        int i1 = informationMapper.insertSelective(information);
        return i+i1;
    }

    @Override
    public Carpay selectByPrimaryKey(Long id) {
        return carpayMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加车辆缴费信息
     * @param record
     * @return
     */
    @Override
    public int insertSelective(Carpay record) {
        return carpayMapper.insertSelective(record);
    }

    /**
     * 修改车辆缴费信息
     * @param record
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Carpay record) {
        return carpayMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Carpay record) {
        return 0;
    }
}
